package com.banksoft.front.controller;

import com.banksoft.admin.SystemCache;
import com.banksoft.admin.dao.AdvertDao;
import com.banksoft.admin.dao.ProductDao;
import com.banksoft.admin.entity.*;
import com.banksoft.admin.service.*;
import com.banksoft.common.BaseController;
import com.banksoft.util.StringNumber;
import com.banksoft.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by vanjoger on 2017/1/2.
 */
@Controller
@RequestMapping("/front")
public class FrontProductController extends BaseController {

    @Autowired
    ProductService productService;
    @Autowired
    BaseService baseService;
    @Autowired
    UserAddressService userAddressService;
    @Autowired
    UserService userService;
    @Autowired
    UserMoneyService userMoneyService;

    @RequestMapping("/productList")
    public String productList(Model model, HttpServletResponse response,HttpServletRequest request){
        try {
          /*  String openId = "oRr7vvwBpTJ1dcvKO6tbDOT2S6h4";
            User user = this.userService.findByOpenId(openId);
            setSession(SystemCache.SESSION_USER,user);*/

            User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);
            model.addAttribute("user", user);
        }catch (Exception e){
            e.printStackTrace();
        }

        List<Product> list = this.productService.listProduct();

        List<Advert> advertlist = this.baseService.findAll(AdvertDao.class);
        model.addAttribute("adverts",advertlist);
        model.addAttribute("data",list);
        return "front/front_product_list";
    }

    @RequestMapping("/productDetail")
    public String productDetail(String id,Model model){
        Product product = this.baseService.findById(ProductDao.class,id);
        model.addAttribute("data",product);

        String images = product.getImages();
        if(StringUtil.isNotNull(images)){
            String[] arrays = images.split("\\|");
            if(arrays.length>2) {
                model.addAttribute("headImg", arrays[0]);
                model.addAttribute("footImg",arrays[arrays.length-1]);
                model.addAttribute("images",arrays);
            }else if(arrays.length==2){
                model.addAttribute("headImg", arrays[0]);
                model.addAttribute("footImg",arrays[1]);
            }else if(arrays.length==1){
                model.addAttribute("headImg", arrays[0]);
            }
        }
        return "front/front_product_detail";
    }

    @RequestMapping("/cartList")
    public String cartList(Model model, HttpServletRequest request,String id){
        List<Product> list = new ArrayList<>();
        Product product = this.baseService.findById(ProductDao.class,id);
        list.add(product);
                //this.productService.listProduct();
        model.addAttribute("data",list);

        /*String openId = this.getCookie(SystemCache.SESSION_USER,request);*/
        User user = (User) request.getSession().getAttribute(SystemCache.SESSION_USER);
        UserMoney money = this.userMoneyService.findByUserId(user.getId());
        if(money!=null) {
            model.addAttribute("money", StringNumber.add(money.getCash(),money.getBonus()));
        }else{
            model.addAttribute("money", "0");
        }

        List<UserAddress> addressList = this.userAddressService.listByUserId(user.getId());
        if(StringUtil.isNotEmpty(addressList))
            model.addAttribute("userAddress",addressList.get(0));

        return "front/front_cart_list";
    }

}
